﻿@model CRM.Infrastructure.Entity.Users
@{
    ViewBag.Title = "添加用户";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<section class="content-header">
    <h1>
        用户管理
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
        <li><a href="#" id="preBtn">用户管理</a></li>
        <li class="active">添加用户</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header">
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <form class="form-horizontal" id="form1">
                        <div class="box-body">
                            @*管理员管理页面进来，才能对公司信息做修改和添加*@
                            @if (Model.RoleID == 1)
                            {
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">公司ID</label>
                                    <div class="col-sm-3">
                                        @if (Model.CompanyID > 0)
                                        {
                                            <input type="text" name="CompanyID" disabled="disabled" id="CompanyID" value="@Model.CompanyID" class="form-control" maxlength="20">
                                        }
                                        else
                                        {
                                            <input type="text" name="CompanyID" id="CompanyID" value="@Model.CompanyID" class="form-control" maxlength="20">
                                        }

                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">公司名称</label>
                                    <div class="col-sm-3">
                                        <input type="text" name="CompanyName" id="CompanyName" value="@Model.CompanyName" class="form-control" maxlength="20">
                                    </div>
                                </div>
                            }
                            <div class="form-group">
                                <label class="col-sm-2 control-label">名称</label>
                                <div class="col-sm-3">
                                    <input type="text" name="UserName" id="UserName" value="@Model.UserName" class="form-control" maxlength="20">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">密码</label>
                                <div class="col-sm-3">
                                    <input type="password" name="PassWord" id="PassWord" value="@Model.PassWord" class="form-control">
                                </div>
                                <div class="col-sm-3" style="display:none" id="pwdHid">
                                    <p class="help-block">默认密码:123456</p>

                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">电话</label>
                                <div class="col-sm-3">
                                    <input type="text" name="Mobile" id="Mobile" value="@Model.Mobile" class="form-control">
                                </div>
                            </div>
                            <!-- /.box-body -->
                            <div class="box-footer text-center">
                                <button class="btn btn-default" type="button" id="btnBack">取消</button>
                                <button class="btn btn-info" type="button" id="btnSave">保存</button>
                            </div>
                            <!-- /.box-footer -->
                    </form>
                </div>
                <div class="box-footer clearfix ">
                </div>
                <!-- /.box-body -->
                <!-- /.box -->
            </div>
        </div>
    </div>
</section>
@section scriptsBottom{
    <script type="text/javascript">
        var userId = 0; //是否是添加页面，如果是编辑页则判断条件增加ID
        $(document).ready(function () {
            initFormValidate();
            if (@Model.UserID> 0) {
                $("#pwdHid").hide();
            } else {
                $("#pwdHid").show();
            }
            //保存
            $("#btnSave").click(function () {
                save();
            });

            //保存
            $("#btnBack").click(function () {
                var url = '@Url.Action("UserList", "User")';
                location.href = url;
            });


            $("#preBtn").click(function () {
                var url = '@Url.Action("UserList", "User")';
                location.href = url;
            })
        });

        //保存
        function save() {
            var bootstrapValidator = $("#form1").data('bootstrapValidator');
            bootstrapValidator.validate();
            if (bootstrapValidator.isValid()) {
                var url = '@Url.Action("Add", "User")';
                var options = {
                    url: url,
                    type: "POST",
                    data: {
                        UserID: @Model.UserID,
                        RoleID:@Model.RoleID,
                        UserName: $("#UserName").val(),
                        PassWord: $("#PassWord").val(),
                        Mobile: $("#Mobile").val(),
                        CompanyID: $("#CompanyID").val() || -1,
                        CompanyName:$("#CompanyName").val()||""
                    },
                    success: function (o) {
                        if (o.status == 0) {
                            BMCW.msg("保存成功", function () {
                                location.href='@Url.Action("UserList", "User")';
                            });
                        } else {
                            BMCW.alert(o.message);
                        }
                    }
                };

                BMCW.ajax(options);
            }
        }

        function initFormValidate() {
            $("#form1").bootstrapValidator({
                submitHandler: function (valiadtor, loginForm, submitButton) {
                    valiadtor.defaultSubmit();
                },
                fields: {
                    UserName: {
                        validators: {
                            notEmpty: {
                                message: '请输入姓名'
                            }
                        }
                    },
                    PassWord: {
                        validators: {
                            stringLength: {
                                min: 6,
                                max: 18,
                                message: '密码长度必须在6到18位之间'
                            },
                        }
                    },
                    Mobile: {
                        validators: {
                            notEmpty: {
                                message: '请输入电话号码'
                            },
                            phone: {
                                country: 'CN',
                                message: '请输入正确的电话号码'
                            }
                        }
                    }
                }
            });
        }
    </script>
}




